<?php

declare (strict_types=1);

namespace App\Model;

use Hyperf\DbConnection\Db;

/**
 * @property int $id
 * @property int $uid
 * @property int $duration
 * @property string $currency
 * @property string $price
 * @property int $status
 */
class ConsultantGood extends Model
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'consultant_goods';
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $guarded = [];
    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = ['id' => 'integer', 'uid' => 'integer', 'duration' => 'integer', 'status' => 'integer'];

    public static function updatePrice($uid, $classId, $price, $country)
    {
        //后面 抽象方法
        $currency = 'USD';
        if ($country == 53) {

            $currency = 'CNY';
        }

        $duration = ClassHour::where(['id' => $classId])->value('duration');

        $consultantGoods = [
            'uid' => $uid,
            'duration' => $duration,
            'currency' => $currency,
            'price' => $price,
            'status' => 1,
            'create_time' => time()
        ];


        Db::beginTransaction();
        try {
            self::where(['uid' => $uid])->update(['status' => 0, 'create_time' => time()]);

            $goodsId = self::insertGetId($consultantGoods);
            $consultantGoodsLog = [
                'goods_id' => $goodsId,
                'uid' => $uid,
                'currency' => $currency,
                'price' => $price,
                'status' => 1,
                'create_time' => time(),
            ];

            ConsultantGoodsLog::insert($consultantGoodsLog);

            Db::commit();
            return true;
        } catch (\Exception $e) {
            var_dump($e->getMessage());
            Db::rollBack();
            return false;
        }


    }
}